package cn.zw.frr.ringbuffer;

import cn.zw.frr.service.IEventService;
import cn.zw.frr.factory.DataDTO;
import cn.zw.frr.factory.DataEvent;
import com.github.houbb.heaven.reflect.api.IMethod;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.WorkHandler;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class EventCommond extends BaseCommond<DataDTO, DataEvent, EventHandler>{

    /**
     * 队列容积：必须为2*N次方，可使用1024*n表示
     */
    private static final int QUEUE_SIZE = 1048576;


    IEventService iEventService;


    private static ConcurrentHashMap<Integer,IEventService> map = new ConcurrentHashMap<>();


    String methodName;
    Class param;


    public EventCommond() {
    }

    public BaseCommond setiEventService(IEventService iEventService) {
        this.iEventService = iEventService;
        return this;
    }

    public BaseCommond setiEventService(Integer num ,IEventService iEventService) {
        map.put(num,iEventService);
        return this;
    }


    @Override
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    @Override
    public void setParam(Class param) {
        this.param = param;
    }

    @Override
    protected int getQueueSize() {
        return QUEUE_SIZE;
    }

    @Override
    protected com.lmax.disruptor.EventFactory eventFactory() {
       return new cn.zw.frr.factory.EventFactory();
    }



    @Override
    protected WaitStrategy getStrategy() {
        return new BlockingWaitStrategy();
    }

    @Override
    protected WorkHandler[] getHandler(int count) {
        ArrayList<EventHandler> outEventHandlers = new ArrayList<>();
        if(map.size()==0){

            if(methodName!=null){
                if(param!=null){
                    for (int i = 0; i < count; i++) {
                        System.out.println(5);
                        outEventHandlers.add(new EventHandler(iEventService,methodName,param));
                    }
                }else{
                    for (int i = 0; i < count; i++) {
                        outEventHandlers.add(new EventHandler(iEventService,methodName));
                    }
                }
            }else{
                for (int i = 0; i < count; i++) {
                    outEventHandlers.add(new EventHandler(iEventService));
                }
            }

        }
        else{
            if(methodName!=null){
                if(param!=null){
                    for (int i = 0; i < count; i++) {
                        outEventHandlers.add(new EventHandler(iBaseServiceNum,map,iEventService,methodName,param));
                    }
                }else{
                    for (int i = 0; i < count; i++) {
                        outEventHandlers.add(new EventHandler(iBaseServiceNum,map,iEventService,methodName));
                    }
                }
            }

        }


        int size = outEventHandlers.size();
        EventHandler[] paramEventHandlers = (EventHandler[]) outEventHandlers.toArray(new EventHandler[size]);
        return paramEventHandlers;
    }


}